package cc.booku.owlbox.UserSettingsPage;

import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

import cc.booku.owlbox.ArtDesign.SystemUtil;
import cc.booku.owlbox.R;
import cc.booku.owlbox.loadSqlite.DatabaseHelper;


public class DataClearActivity extends AppCompatActivity {


    private RadioButton setting_history, setting_down, setting_love, setting_user;

    private DatabaseHelper helper;
    private SQLiteDatabase db;

    private Toolbar sToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_clear);
        SystemUtil.setStatusBarColor(this, Color.parseColor("#448aca"));
        initsqlite();

        init_RadioButtonview();

        sToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(sToolbar);//利用Toolbar代替ActionBar
        sToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        sToolbar.inflateMenu(R.menu.menu_save);
        //添加menu 菜单点击事件
        sToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_save:

                        init_ok();

                        break;

                }
                return true;
            }
        });


    }

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        getMenuInflater().inflate(R.menu.menu_save, menu);//toolbar添加menu菜单
        return true;
    }


    private void initsqlite() {

        //5.实例化SQLiteOpenHelper的子类对象-MyDataBaseHelper
        helper = new DatabaseHelper(this, "dbdate", null, R.string.db_version);//dbName数据库名
        db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
    }

    private void init_RadioButtonview() {

        setting_history = (RadioButton) findViewById(R.id.setting_history);
        setting_down = (RadioButton) findViewById(R.id.setting_down);
        setting_love = (RadioButton) findViewById(R.id.setting_love);
        setting_user = (RadioButton) findViewById(R.id.setting_user);
    }


    private void init_ok() {

        if (setting_history.isChecked()) {

            db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
            // db.delete("collectTable", null, null);

            Toast.makeText(DataClearActivity.this, "成功删除" + db.delete("collectTable", null, null) + "行", Toast.LENGTH_SHORT).show();

            db.close();

        }
        if (setting_down.isChecked()) {

            db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象

            Toast.makeText(DataClearActivity.this, "成功删除" + db.delete("downTable", null, null) + "行", Toast.LENGTH_SHORT).show();
            db.close();

        }
        if (setting_love.isChecked()) {

            db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
            //db.delete("loveTable", null, null);
            Toast.makeText(DataClearActivity.this, "成功删除" + db.delete("loveTable", null, null) + "行", Toast.LENGTH_SHORT).show();
            db.close();

        }

        if (setting_user.isChecked()) {

            db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
            // db.delete("collectTable", null, null);

            Toast.makeText(DataClearActivity.this, "成功删除" + db.delete("userTable", null, null) + "行", Toast.LENGTH_SHORT).show();

            db.close();

        }

        Toast.makeText(DataClearActivity.this, "已保存回到用户页面", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 2000);


    }


}
